home *** CD-ROM | disk | FTP | other *** search
- package sub_arctic.lib;
-
- import sub_arctic.output.style_manager;
- import sub_arctic.output.color_pair;
- import sub_arctic.output.drawable;
- import java.awt.Font;
- import java.awt.Color;
- import java.awt.Dimension;
-
- /**
- * This is a class which draws a string on the screen. This object has
- * options for putting a box around the label, making it be opaque,
- * setting its colors, and of course controlling the displayed string.
- *
- * @author Scott Hudson
- */
- public class label extends oneline_text_display {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Are we drawing an opaque background? */
- protected boolean _opaque = false;
-
- /**
- * Are we drawing an opaque background?
- * @return boolean true if the background is cleared before drawing
- */
- public boolean opaque() {return _opaque;}
-
- /**
- * Set whether we are drawing an opaque background.
- * @param boolean v the new opacity truth
- */
- public void set_opaque(boolean v)
- {
- if (v != _opaque)
- {
- _opaque = v;
- damage_self();
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This stores the above spacing of the interactor. By default it
- * is three pixels, just like online_text_display.
- */
- protected int _above_spacing=3;
-
- /**
- * Get the spacing above the text of this interactor. Since this
- * interactor has above and below spacing, the v_spacing attribute
- * of its superclass is ignored.
- *
- * @return int the number of pixels above this interactor
- */
- public int above_spacing() { return _above_spacing;}
-
- /*
- * Set the spacing above this text of this interactor. This
- * function and set_below_spacing() supersede the use of set_v_spacing
- * on this interactor.
- * @param int as the new spacing above the interactor
- */
- public void set_above_spacing(int as) {
- _above_spacing=as;
- calculate_size();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This stores the below spacing of the interactor. By default it
- * is three pixels, just like online_text_display.
- */
- protected int _below_spacing=3;
-
- /**
- * Get the spacing below the text of this interactor. Since this
- * interactor has above and below spacing, the v_spacing attribute
- * of its superclass is ignored.
- *
- * @return int the number of pixels below this interactor
- */
- public int below_spacing() { return _below_spacing;}
-
- /*
- * Set the spacing below this text of this interactor. This
- * function and set_above_spacing() supersede the use of set_v_spacing
- * on this interactor.
- * @param int as the new spacing above the interactor
- */
- public void set_below_spacing(int bs) {
- _below_spacing=bs;
- calculate_size();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * When this object changes its spacing attributes, we may need
- * recalculate its intrinsic size. This routine does that.
- */
- public void calculate_size() {
- /* if we are autosizing, nobody cares */
- if (!_autosize) return;
- /* height of the font + the above and below */
- set_intrinsic_h(_metric.getAscent() + _above_spacing +
- _below_spacing);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Color pair that we draw with. */
- protected color_pair _draw_colors = manager.default_color_pair();
-
- /**
- * Retrieve the color pair that we draw with.
- * @return color_pair the current color pair we are using
- */
- public color_pair draw_colors() {return _draw_colors;}
-
- /**
- * Set the color pair that we draw with.
- * @param color_pair c the new color pair to use
- */
- public void set_draw_colors(color_pair c)
- {
- if (c != null)
- {
- _draw_colors = c;
- damage_self();
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Make a label using some simple defaults. It doesn't try to position
- * itself. It assumes you'll do this with constraints. It sizes by
- * the size of the text.
- *
- * @param String s the string to put in the label
- */
- public label(String s) {
- super(0,0,s);
- set_boxed(false);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Make a label with some defaults. This time you supply the
- * width. Assumes x and y will use constraints.
- *
- * @param String s the string to display
- * @param int width the width of the displayable area in pixels
- */
- public label(String s, int width) {
- super(0,0,width,s,style_manager.default_font(),true);
- set_boxed(false);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Make a label with some defaults. This time you supply the
- * width & a font. Assumes x and y will use constraints.
- *
- * @param String s the displayed string
- * @param int width the number of pixels wide the display area is
- * @param Font f the font render the string in
- */
- public label(String s, int width, Font f) {
- super(0,0,width,s,f,true);
- set_boxed(false);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Make a label with some defaults. This time you supply the
- * font. Assumes x and y will use constraints.
- *
- * @param String s the displayed string
- * @param Font f the font to use for rendering the string
- */
- public label(String s, Font f) {
- super(0,0,s,f,true);
- set_boxed(false);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Draw the display
- * @param drawable d the drawable on which to do the display
- */
- protected void draw_self_local(drawable d)
- {
- int x,y;
-
- /* clear the background if they asked for that */
- if (opaque())
- {
- d.setColor(draw_colors().background());
- d.fillRect(0,0,w()-1,h()-1);
- }
-
- /* set font to our font, and start drawing in foreground */
- d.setFont(font());
- d.setColor(draw_colors().foreground());
-
- /* draw the text */
- if (_autosize) {
- d.drawString(text(), _h_spacing, _above_spacing+_metric.getAscent());
- } else {
- x=(w()-_metric.stringWidth(text()))/2;
- y=(h()-(_metric.getHeight() - _metric.getLeading()))/2;
- d.drawString(text(),x,y+_metric.getAscent());
- }
-
- /* draw box if requested */
- if (boxed())
- d.drawRect(0,0,w()-1,h()-1);
- }
-
- //had:
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * We override the implementation of set_text() for this object and
- * reset the width. The reason we override this is because the parent
- * class oneline_text_display doesn't want to resize every time its
- * text changes... that would be distracting. Label's however do
- * want to resize every time.
- *
- * @param String t the string that is being displayed
- */
- public void set_text(String t) {
- super.set_text(t);
- if (_autosize) {
- set_w(_metric.stringWidth(text()) + 2*_h_spacing);
- }
- damage_self();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This is the storage for the autosize variable.
- */
- protected boolean _autosize=true;
-
- /**
- * Retrieve the current state of autosize.
- * @return boolean true if the object is autosized (and thus not resizable)
- */
- public boolean autosize() { return _autosize;}
-
- /**
- * Set the current state of the autosize variable. If you change the
- * state from non-autosized (false) to autosized (true) the label
- * will immediately autosize itself. If you change the state to
- * non-autosized from autosized, no immediate redraw is requested
- * and the object will continue at its current size unless that
- * is changed.
- */
- public void set_autosize(boolean a) {
- Dimension d;
-
- /* no work to do */
- if (a==_autosize) {
- return;
- }
- /* save it */
- _autosize=a;
- /* is it currently false and now true? */
- if (a==true) {
- d=natural_size();
- set_h(d.height);
- set_w(d.width);
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * We override the intrinsic constraint function because sometimes
- * we have intrinsic constraints (when autosize is on) and sometimes
- * we don't.
- * @return int a constant representing what (if any) our intrinsic
- * constraints are.
- */
- public int intrinsic_constraints() {
- if (_autosize) {
- return H;
- } else {
- return 0;
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This function computes the natural size of a label. This is useful
- * when you want to walk down a list (or more likely, column) of labels
- * and find out which of them has the largest natural size and then
- * set all of them to that size.
- * @return Dimension the size (in pixels) of this button at its natural size
- */
- public Dimension natural_size() {
- int height=(_metric.getHeight()-_metric.getLeading()) + (2*_v_spacing);
- int width=(_metric.stringWidth(text()))+ (2*_h_spacing);
- return new Dimension(width,height);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * We have to override this to avoid getting a problem when the
- * superclass tries to set the intrinsic height. We don't bother
- * setting the intrinsic height when we are not autosized.
- *
- * @param int h the new intrinsic height
- */
- public void set_intrinsic_h(int h) {
- if (_autosize) {
- super.set_intrinsic_h(h);
- } else {
- /* normal set h */
- set_h(h);
- }
- }
-
- //had:
- //* @exception cannot_assign PROPAGATED
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-